#include "binaryTree.h"

/**
 * 获取二叉树节点
 */
BTNode*
buyNode(int value)
{
  BTNode* node = (BTNode*)malloc(sizeof(BTNode));
  if (!node) {
    perror("malloc failed.\n");
    exit(EXIT_FAILURE);
  }
  node->value = value;
  node->left = node->right = NULL;

  return node;
}

/**
 * 手动构建一颗二叉树(硬核！)
 */
BTNode*
buildBT()
{
  BTNode* n1 = buyNode(1);
  BTNode* n2 = buyNode(2);
  BTNode* n3 = buyNode(3);
  BTNode* n4 = buyNode(4);
  BTNode* n5 = buyNode(5);
  BTNode* n6 = buyNode(6);
  n1->left = n2;
  n1->right = n4;
  n2->left = n3;
  n4->left = n5;
  n4->right = n6;

  return n1;
}

/**
 * 测试二叉树遍历
 */
void
test()
{
  BTNode* root = buildBT();
  printf("前序遍历：");
  PreOrder(root);
  printf("\n中序遍历：");
  InOrder(root);
  printf("\n后续遍历：");
  PostOrder(root);
  printf("\n");

  printf("节点个数：%d\n", TreeSize(root));

  printf("叶子节点个数：%d\n", TreeLeaf(root));

  printf("二叉树的高度：%d\n", TreeHeight(root));

  printf("第3层节点个数：%d\n", TreeKSize(root, 3));
}

int
main()
{
  test();
  return EXIT_SUCCESS;
}